﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AviumX.AviumCore.Hardware.Mouse
{
    class Cursor
    {
        /*
        * Note: since the buffer is always cleared to a color we really dont need to 
        * save what is behind the mouse anymore, so we are just going to draw it. =)
        */

        /*
         * Should work perfect with the new double buffer code
         * and should have no major problems. - Civilwarrock
         */

        //Handlers, used to work with the mouse.
        public static AviumCore.Screen.Graphics.Images.Mouse CursorImage = new AviumCore.Screen.Graphics.Images.Mouse();
        public static AviumCore.Screen.Graphics.Images.IBeam IBeamImage = new AviumCore.Screen.Graphics.Images.IBeam();
        public static AviumCore.Hardware.Mouse.Mouse Mouse = new AviumCore.Hardware.Mouse.Mouse();
        /*
         * Mouse Sates
         */
        public enum MouseState
        {
            LeftClick,
            RightClick,
            MiddleClick,
            Nothing
        }
        public enum CursorType
        {
            Standard,
            IBeam
        }
        //Current State.
        public static MouseState CurrentState = MouseState.Nothing;
        public static CursorType CurrentCursor = CursorType.Standard;
        //Location handlers
        private static int x = 0;
        private static int y = 0;
        #region " Drawing Handlers "
        public static void StartCursor()
        {
            Mouse.Start();
            x = Mouse.Getx();
            y = Mouse.Gety();
            CurrentCursor = CursorType.Standard;
            CursorImage.UpdateLocation(x, y);
        }
        public static void DrawCursor()
        {
            /*
             * Incase the mouse has not been drawn yet,
             * and something got drawn before it and then it got drawn
             * we want to save the back buffer to prevent image loss, and
             * some other weird effects.
             */

            #region " Click Events Handlers "
            if (Mouse.CheckLeftClick() == true)
            {
                CurrentState = MouseState.LeftClick;
            }
            else if (Mouse.CheckRightClick() == true)
            {
                CurrentState = MouseState.RightClick;
            }
            else if (Mouse.CheckMiddleClick() == true)
            {
                CurrentState = MouseState.MiddleClick;
            }
            else
            {
                CurrentState = MouseState.Nothing;
            }
            #endregion
            x = Mouse.Getx();
            y = Mouse.Gety();
            if (CurrentCursor == CursorType.Standard)
            {
                if (x > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - CursorImage.width && y > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - CursorImage.height)
                {
                    CursorImage.UpdateLocation(AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - CursorImage.width, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - CursorImage.height);
                    AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBlackAndWhiteInBuffer(CursorImage.CursorImage, CursorImage.width, CursorImage.height, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - CursorImage.width, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - CursorImage.height);
                }
                else if (y > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - CursorImage.height)
                {
                    CursorImage.UpdateLocation(x, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - CursorImage.height);
                    AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBlackAndWhiteInBuffer(CursorImage.CursorImage, CursorImage.width, CursorImage.height, x, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - CursorImage.height);
                }
                else if (x > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - CursorImage.width)
                {
                    CursorImage.UpdateLocation(AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - CursorImage.width, y);
                    AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBlackAndWhiteInBuffer(CursorImage.CursorImage, CursorImage.width, CursorImage.height, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - CursorImage.width, y);
                }
                else
                {
                    CursorImage.UpdateLocation(x, y);
                    AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBlackAndWhiteInBuffer(CursorImage.CursorImage, CursorImage.width, CursorImage.height, x, y);
                }
            }
            else if (CurrentCursor == CursorType.IBeam)
            {
                if (x > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - IBeamImage.width && y > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - IBeamImage.height)
                {
                    IBeamImage.UpdateLocation(AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - IBeamImage.width, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - IBeamImage.height);
                    AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBlackAndWhiteInBuffer(IBeamImage.IBeamImage, IBeamImage.width, IBeamImage.height, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - IBeamImage.width, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - IBeamImage.height);
                }
                else if (y > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - IBeamImage.height)
                {
                    IBeamImage.UpdateLocation(x, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - IBeamImage.height);
                    AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBlackAndWhiteInBuffer(IBeamImage.IBeamImage, IBeamImage.width, IBeamImage.height, x, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - IBeamImage.height);
                }
                else if (x > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - IBeamImage.width)
                {
                    IBeamImage.UpdateLocation(AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - IBeamImage.width, y);
                    AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBlackAndWhiteInBuffer(IBeamImage.IBeamImage, IBeamImage.width, IBeamImage.height, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - IBeamImage.width, y);
                }
                else
                {
                    IBeamImage.UpdateLocation(x, y);
                    AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBlackAndWhiteInBuffer(IBeamImage.IBeamImage, IBeamImage.width, IBeamImage.height, x, y);
                }
            }
        }
        #endregion
        #region " Click Handlers "
        public static Boolean CheckMouseLeftClick(int x, int y, int width, int height)
        {
            if (Mouse.Getx() >= x && Mouse.Getx() <= x + width && Mouse.Gety() >= y && Mouse.Gety() <= y + height && CurrentState == MouseState.LeftClick)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static Boolean CheckMouseRightClick(int x, int y, int width, int height)
        {
            if (Mouse.Getx() >= x && Mouse.Getx() <= x + width && Mouse.Gety() >= y && Mouse.Gety() <= y + height && CurrentState == MouseState.RightClick)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static Boolean CheckMouseMiddleClick(int x, int y, int width, int height)
        {
            if (Mouse.Getx() >= x && Mouse.Getx() <= x + width && Mouse.Gety() >= y && Mouse.Gety() <= y + height && CurrentState == MouseState.MiddleClick)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
    }
}
